﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using DbMetadata.Providers;
using DbMetadata.Providers.SqlServer;

namespace DbMetadata
{
    /// <summary>
    /// Extends <see cref="IDbConnection"/> with the ability to retrieve database metadata.
    /// </summary>
    public static class DbConnectionExtensions
    {
        
        /// <summary>
        /// Returns a <see cref="DbMetadataInfo"/> instance that represents the database metadata.
        /// </summary>
        /// <returns><see cref="DbMetadataInfo"/></returns>
        public static DbMetadataInfo GetMetadata(this IDbConnection dbConnection)
        {
            var metadataProvider = CreateMetadataProvider(dbConnection);
            return metadataProvider.GetMetadata();
        }
                
        private static DbMetadataProvider CreateMetadataProvider(this IDbConnection dbConnection)
        {
            IDbMetadataProviderFactory dbMetadataProviderFactory =
                DbMetadataProviderFactories.GetMetadataProviderFactory(dbConnection.GetType().FullName);

            return dbMetadataProviderFactory.CreateMetadataProvider(dbConnection);
        }        
    }
}
